package com.ca.ui.core.view.bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.ImageUtils;

public class MainTitleBar extends ConstraintLayout {

    private String headerUrl;
    private Drawable lightSearchIcon;
    private Drawable darkSearchIcon;
    private Drawable lightNotifyIcon;
    private Drawable darkNotifyIcon;
    private int type;

    public ImageView headerView;
    public MiniSearchBar searchBar;
    public ImageView notifyIcon;

    public MainTitleBar(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public MainTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public MainTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.main_title_bar_layout, this);
        headerView = view.findViewById(R.id.iv_header);
        searchBar = view.findViewById(R.id.bar_search);
        notifyIcon = view.findViewById(R.id.iv_notify);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MainTitleBar, 0, 0);

            headerUrl = array.getString(R.styleable.MainTitleBar_mtb_headerUrl);
            lightSearchIcon = array.getDrawable(R.styleable.MainTitleBar_mtb_lightSearchIcon);
            darkSearchIcon = array.getDrawable(R.styleable.MainTitleBar_mtb_darkSearchIcon);
            lightNotifyIcon = array.getDrawable(R.styleable.MainTitleBar_mtb_lightNotifyIcon);
            darkNotifyIcon = array.getDrawable(R.styleable.MainTitleBar_mtb_darkNotifyIcon);
            type = array.getInteger(R.styleable.MainTitleBar_mtb_type, 0);

            array.recycle();
        }

        setHeaderUrl(headerUrl);
        setLightSearchIcon(lightSearchIcon);
        setDarkSearchIcon(darkSearchIcon);
        setType(type);
    }

    public String getHeaderUrl() {
        return headerUrl;
    }

    public void setHeaderUrl(String headerUrl) {
        this.headerUrl = headerUrl == null ? "" : headerUrl;
        ImageUtils.loadCircleImage(getContext(), this.headerUrl, headerView);
    }

    public Drawable getLightSearchIcon() {
        return lightSearchIcon;
    }

    public void setLightSearchIcon(Drawable lightSearchIcon) {
        this.lightSearchIcon = lightSearchIcon;
        searchBar.setLightIcon(lightSearchIcon);
    }

    public Drawable getDarkSearchIcon() {
        return darkSearchIcon;
    }

    public void setDarkSearchIcon(Drawable darkSearchIcon) {
        this.darkSearchIcon = darkSearchIcon;
        searchBar.setDarkIcon(darkSearchIcon);
    }

    public Drawable getLightNotifyIcon() {
        return lightNotifyIcon;
    }

    public void setLightNotifyIcon(Drawable lightNotifyIcon) {
        this.lightNotifyIcon = lightNotifyIcon;
        setType(type);
    }

    public Drawable getDarkNotifyIcon() {
        return darkNotifyIcon;
    }

    public void setDarkNotifyIcon(Drawable darkNotifyIcon) {
        this.darkNotifyIcon = darkNotifyIcon;
        setType(type);
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
        if (this.type == 0) {
            notifyIcon.setImageDrawable(lightNotifyIcon);
        }
        else {
            this.type = 1;
            notifyIcon.setImageDrawable(darkNotifyIcon);
        }

        searchBar.setType(this.type);
    }

    public void light() {
        setType(0);
    }

    public void dark() {
        setType(1);
    }
}
